package main;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import dao.AdminDao;
import dao.AdminDaoFactory;
import dao.StudentDao;
import dao.StudentDaoFactory;
import entity.Admin;
import entity.Student;
import service.ApairService;
import service.ChangeOne;
import service.ChangeTwo;
import service.EnterSch;
import service.HealthService;
import service.LeaveSch;
import service.VisitService;
import service.findStu;
import service.impl.GroupManagementImp;
import service.impl.ApairServiceImpl;
import service.impl.ChangeOneImpl;
import service.impl.ChangeTwoImpl;
import service.impl.EnterSchImpl;
import service.impl.FindStuImpl;
import service.impl.HealthServiceImpl;
import service.impl.LeaveSchImpl;
import service.impl.VisitServiceImpl;

public class Main {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		start();
	}
	public static void start() {
		boolean flag = true;
		Scanner scanner = new Scanner(System.in);
		
		do {
			System.out.println("欢迎来到凌水宿舍管理系统1.0!");
			System.out.println("1. 管理员登陆");
			System.out.println("2. 学生登陆");
			System.out.println("3. 新生注册");
			System.out.println("4. 访客登记");
			System.out.println("5. 查看报修记录");
			System.out.println("6. 卫生情况公示");
			System.out.println("7. 违纪同学公示");
			System.out.println("8. 退出系统");
			System.out.println("请输入选择");
			int select = scanner.nextInt();
			switch (select) {
			case 1:
				adminDesktop();
				break;
			case 2:
				stuDesktop();
				break;
			case 3:
				registerStu();
				break;
			case 4:
				VisitService visitService = new VisitServiceImpl();
				visitService.visitRegist();
				break;
			case 5:
				ApairService apairService = new ApairServiceImpl();
				apairService.lookAllApair();
				break;
			case 6:
				HealthService healthService = new HealthServiceImpl();
				healthService.lookAll();
				break;
			case 7:
				GroupManagementImp g1 = new GroupManagementImp(); 
				g1.chaqinPublic();
				break;
			case 8:
				flag = false;
				break;
			default:
				break;
			}
			
		} while (flag);
		System.out.println("感谢使用凌水宿舍管理系统1.0!");
	}
	

	public static void adminDesktop() {
		Scanner scanner = new Scanner(System.in);
		Admin admin = adminLogin();
		if(admin == null)
			return;
		System.out.println("欢迎进入管理员菜单!");
		boolean flag = true;
		do {
			System.out.println("1. 查看学生信息");
			System.out.println("2. 调整宿舍菜单");
			System.out.println("3. 修改密码");
			System.out.println("4. 查看访客记录");
			System.out.println("5. 处理报修问题");
			System.out.println("6. 卫生检查菜单");
			System.out.println("7. 查寝情况记录");
			System.out.println("8. 批量管理");
			System.out.println("9. 退出");
			System.out.println("请选择");
			int select = scanner.nextInt();
			switch (select) {
			case 1:
				lookStuOrDorm();
				break;
			case 2:
				adjustDorm();
				break;
			case 3:
				changePwd(admin);
				break;
			case 4:
				VisitService visitService = new VisitServiceImpl();
				visitService.visit();
				break;
			case 5:
				ApairService apairService = new ApairServiceImpl();
				boolean b = apairService.lookNewApair(admin);
				if(b) {
					apairService.processApair(admin);
				}
				else {
					System.out.println("没有要处理的报修单,退出");
				}
				break;
			case 6:
				HealthService healthService = new HealthServiceImpl();
				healthService.insertHealth();
				break;
			case 7:
				GroupManagementImp g1 = new GroupManagementImp(); 
				g1.setChaQin();
				break;
			case 9:
				flag = false;
				break;
			case 8:
				groupManage();
				break;
			default:
				break;
			}
		} while (flag);
	}

	public static void stuDesktop() {
		Scanner scanner = new Scanner(System.in);
		Student student = studentLogin();
		if(student == null)
			return;
		System.out.println("欢迎进入学生菜单");
		boolean flag = true;
		do {
			System.out.println("1. 查看学生信息");
			System.out.println("2. 修改登陆密码");
			System.out.println("3. 查看卫生情况");
			System.out.println("4. 查看访客记录");
			System.out.println("5. 报修菜单");
			System.out.println("6. 申请退宿");
			System.out.println("7. 退出");
			System.out.println("请选择");
			int select = scanner.nextInt();
			switch (select) {
			case 1:
				LookOwnInfo(student);
				break;
			case 2:
				changePwd(student);
				break;
			case 3:
				lookOwnHealth(student);
				break;
			case 4:
				VisitService visitService = new VisitServiceImpl();
				visitService.visit(student.getId());
				break;
			case 5:
				System.out.println("1. 查看报修情况");
				System.out.println("2. 申请报修");
				int se = scanner.nextInt();
				ApairService apairService = new ApairServiceImpl();
				switch (se) {
				case 1:
					apairService.lookOwnApair(student);
					break;
				case 2:
					apairService.addApair(student);
					break;
				default:
					break;
				}
				break;
			case 6:
				LeaveSch leaveSch = new LeaveSchImpl();
				boolean b = leaveSch.leaveSchool(student.getId());
				flag = false;
				break;
			case 7:
				flag = false;
				break;
			default:
				break;
			}
		} while (flag);

	}
	

	private static Student studentLogin() {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学号: ");
		int id = scanner.nextInt();
		System.out.println("请输入密码: ");
		String password = scanner.next();
		StudentDao studentDao = StudentDaoFactory.getStudentDao();
		Student student = studentDao.find(id);
		if(student == null) {
			System.out.println("学生不存在, 退出");
			return null;
		}
		if(!password.equals(student.getPassword())) {
			System.out.println("密码不匹配, 退出");
			return null;
		}
		System.out.println("登陆成功");
		return student;
	}
	public static void registerStu() {
		StudentDao studentDao = StudentDaoFactory.getStudentDao();
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学号");
		int id = scanner.nextInt();
		if( (studentDao.find(id)) != null) {
			System.out.println("学号已注册, 请选择学生登陆");
			return;
		}
		System.out.println("请输入姓名");
		String name = scanner.next();
		System.out.println("请输入性别");
		String sex = scanner.next();
		System.out.println("请输入密码");
		String password = scanner.next();
		Student student = new Student();
		student.setId(id);
		student.setName(name);
		student.setSex(sex);
		student.setPassword(password);
		EnterSch enterSch = new EnterSchImpl();
		boolean b = enterSch.enterSchool(student);
		if(b) {
			System.out.println("注册成功,请选择学生登陆");
		}
		else {
			System.out.println("注册失败, 退出");
		}
	}
	
	private static void LookOwnInfo(Student st) {
		// TODO Auto-generated method stub
		String s = "";
		if(st.getIsHere() == 0) {
			s = "在寝";
		}else if (st.getIsHere() == 1) {
			s = "请假";
		}else {
			s = "违纪, 夜不归寝";
		}
		System.out.println("学号: "+st.getId()+"\t姓名: "+st.getName()+"\t性别: "+st.getSex()+"\t公寓号:"+
					st.getId_apart()+"\t宿舍号: "+st.getId_dorm()+"\t床号: "+st.getId_bed()+"是否在寝: "+
					s);
	}
	
	private static void lookOwnHealth(Student student) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		HealthService healthService = new HealthServiceImpl();
		System.out.println("1. 查看本宿舍所有卫生记录");
		System.out.println("2. 查看某月份卫生记录");
		int select = scanner.nextInt();
		switch (select) {
		case 1:
			healthService.look(student.getId_dorm(), student.getId_apart());
			break;
		case 2:
			System.out.println("请输入月份");
			int month = scanner.nextInt();
			healthService.look(month, student.getId_dorm(), student.getId_apart());
			break;
		default:
			System.out.println("输入错误, 退出");
			break;
		}
	}
	
	private static void changePwd(Student student) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		StudentDao studentDao = StudentDaoFactory.getStudentDao();
		System.out.println("请输入新的登陆密码: ");
		String password = scanner.next();
		System.out.println("请再次确认: ");
		String password1 = scanner.next();
		if(password.equals(student.getPassword())) {
			System.out.println("新密码不能与旧密码相同, 修改密码失败, 退出");
			return ;
		}else if (!password.equals(password1)) {
			System.out.println("两次输入不一致, 修改失败, 退出");
			return ;
		}else {
			student.setPassword(password);
			boolean b = studentDao.update(student);
			if(b) {
				System.out.println("修改密码成功, 退出");
				return;
			}
			System.out.println("修改失败,数据库异常, 退出");
		}
	}

	private static void changePwd(Admin admin) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		AdminDao adminDao = AdminDaoFactory.getAdminDao();
		System.out.println("请输入新的登陆密码: ");
		String password = scanner.next();
		System.out.println("请再次确认: ");
		String password1 = scanner.next();
		if(password.equals(admin.getPassword())) {
			System.out.println("新密码不能与旧密码相同, 修改密码失败, 退出");
			return ;
		}else if (!password.equals(password1)) {
			System.out.println("两次输入不一致, 修改失败, 退出");
			return ;
		}else {
			admin.setPassword(password);
			boolean b = adminDao.update(admin);
			if(b) {
				System.out.println("修改密码成功, 退出");
				return;
			}
			System.out.println("修改失败,数据库异常, 退出");
		}
	}
	public static Admin adminLogin() {
		Admin admin = null;
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入系统管理员的id");
		int id = scanner.nextInt();
		System.out.println("请输入密码");
		String password = scanner.next();
//		Console console = System.console();
//		String password = "";
//		password = new String(console.readPassword());	// 不回显
		AdminDao adminDao = AdminDaoFactory.getAdminDao();
		admin = adminDao.find(id);
		if(admin == null) {
			System.out.println("管理员不存在");
			return null;
		}
		if(admin.getPassword().equals(password)) {
			System.out.println("登陆成功");
			return admin;
		}else {
			System.out.println("密码不匹配");
			return null;
		}

	}
	
	private static void lookStuOrDorm() {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int se = 0;
		System.out.println("1. 查看宿舍里的所有学生信息.");
		System.out.println("2. 查看某一学生信息");
		System.out.println("3. 查看所有学生在寝情况");
		System.out.println("请选择");
		se = scanner.nextInt();
		switch (se) {
		case 3:
			GroupManagementImp g1 = new GroupManagementImp(); 
			g1.stat();
			break;
		case 2:
			System.out.println("请输入学生的公寓号, 宿舍号, 床号:");
			int id_apart = scanner.nextInt();
			int id_dorm = scanner.nextInt();
			int id_bed = scanner.nextInt();
			findStu fstu = new FindStuImpl();
			Student stu = fstu.findStuByDorm(id_bed, id_dorm, id_apart);
			if(stu != null) {
				String s = "";
				if(stu.getIsHere() == 0) {
					s = "在寝";
				}else if (stu.getIsHere() == 1) {
					s = "请假";
				}else {
					s = "违纪, 夜不归寝";
				}
				System.out.println("学号: "+stu.getId()+"\t姓名: "+stu.getName()+"\t性别: "+
				stu.getSex()+"\t是否在寝: "+s);
			}
			break;
		case 1:
			System.out.println("请输入学生的公寓号, 宿舍号:");
			int id_apart1 = scanner.nextInt();
			int id_dorm1 = scanner.nextInt();
			//int id_bed1 = scanner.nextInt();
			findStu fstu1 = new FindStuImpl();
			List<Student> students = fstu1.findStuByDorm(id_dorm1, id_apart1);
			if(students.size() <= 0) {
				System.out.println("宿舍为空");
			}else {
				System.out.println("宿舍共有: "+students.size()+" 人");
				for (Student stu1 : students) {
					String s = "";
					if(stu1.getIsHere() == 0) {
						s = "在寝";
					}else if (stu1.getIsHere() == 1) {
						s = "请假";
					}else {
						s = "违纪, 夜不归寝";
					}
					System.out.println("学号: "+stu1.getId()+"\t姓名: "+stu1.getName()+"\t性别: "+
					stu1.getSex()+"\t是否在寝: "+s);
				}
			}
			break;
		default:
			System.out.println("输入错误, 退出");
			break;
		}
	}
	

	private static void adjustDorm() {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int select = 0;
		boolean flag = true;
		System.out.println("欢迎进入调整宿舍菜单");
		do {
			System.out.println("1. 单人调整宿舍");
			System.out.println("2. 双人调整宿舍");
			System.out.println("3. 宿舍对调");
			System.out.println("请选择: ");
			select = scanner.nextInt();
			boolean b = false;
			int id_stu1 = -1000, id_stu2 = -1000, id_dorm1 = -1000, 
					id_dorm2 = -1000, id_apart1 = -1000, id_apart2 = -1000;
			switch (select) {
			case 1:
				System.out.println("请输入学生学号: ");
				id_stu1 = scanner.nextInt();
				ChangeOne changeOne = new ChangeOneImpl();
				b = changeOne.changeOneStu(id_stu1);
				if(!b) {
					System.out.println("调整失败");
				}else {
					System.out.println("调整成功");
				}
				break;
			case 2:
				System.out.println("请输入两个学生的学号: ");
				id_stu1 = scanner.nextInt();
				id_stu2 = scanner.nextInt();
				ChangeTwo changeTwo = new ChangeTwoImpl();
				b = changeTwo.changeTwoStu(id_stu1, id_stu2);
				break;
			case 3:
				System.out.println("请输入第一个宿舍的公寓号, 宿舍号");
				id_apart1 = scanner.nextInt();
				id_dorm1 = scanner.nextInt();
				System.out.println("请输入第二个宿舍的公寓号, 宿舍号");
				id_apart2 = scanner.nextInt();
				id_dorm2 = scanner.nextInt();
				ChangeTwo changeTwo2 = new ChangeTwoImpl();
				b = changeTwo2.changeTwoDorm(id_dorm1, id_apart1, id_dorm2, id_apart2);
				break;
			default:
				System.out.println("输入错误");
				break;
			}
			System.out.println("是否继续调整, 1.继续 2.退出");
			int se = scanner.nextInt();
			if(se == 2) {
				flag = false;
			}
		} while (flag);
	}

	private static void groupManage() {

		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int select = 0;
		boolean flag = true;
		System.out.println("欢迎进入批量调整菜单");
		do {
			System.out.println("1. 批量入住宿舍");
			System.out.println("2. 批量搬出宿舍");
			System.out.println("请选择: ");
			select = scanner.nextInt();
			switch (select) {
			case 1:
				List<Student> stuList = new ArrayList<Student>();
				stuList.add(StudentDaoFactory.getStudentDao().find(7));
				stuList.add(StudentDaoFactory.getStudentDao().find(8));
				stuList.add(StudentDaoFactory.getStudentDao().find(9));
				stuList.add(StudentDaoFactory.getStudentDao().find(10));
				GroupManagementImp g = new GroupManagementImp(); 
				g.groupIn(stuList);
				break;
			case 2:
				List<Student> stuList1 = new ArrayList<Student>();
				stuList1.add(StudentDaoFactory.getStudentDao().find(7));
				stuList1.add(StudentDaoFactory.getStudentDao().find(8));
				stuList1.add(StudentDaoFactory.getStudentDao().find(9));
				stuList1.add(StudentDaoFactory.getStudentDao().find(10));
				GroupManagementImp g1 = new GroupManagementImp(); 
				g1.gruopOut(stuList1);
				break;
			default:
				System.out.println("输入错误");
				break;
			}
			System.out.println("是否继续调整, 1.继续 2.退出");
			int se = scanner.nextInt();
			if(se == 2) {
				flag = false;
			}
		} while (flag);
	}
}
